import { defineStore } from 'pinia'
import { fetchLoginInfo } from '@/api/auth'

export const useAuth = defineStore('auth', {
  state: () => ({
    token: '',
    userInfo: {
      role_ids: [],
      name: '',
      user_id: '',
      menu_list: []
    }
  }),
  actions: {
    async getUserInfo() {
      if (this.userInfo.role_ids.length) return
      const res = await fetchLoginInfo(this.token)
      this.userInfo.role_ids = res.data.data.role_ids
      this.userInfo.name = res.data.data.name
      this.userInfo.user_id = res.data.data.user_id
      this.userInfo.menu_list = res.data.data.menu_list
    }
  },
  persist: {
    paths: ['token'], // 指定token持久化存储
  }
})
